package com.wyj;

import com.wyj.mapper.DiscussPostMapper;
import com.wyj.service.imp.DiscussPostServiceImp;
import com.wyj.util.RedisUtil;
import lombok.extern.log4j.Log4j2;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.*;

/**
 * @author 王永杰
 * @Description 描述该类的做什么用
 * @since 2022/8/11
 */

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = NiukeltApplication.class)
@Log4j2
public class testThreadPoll {

    //JDK提供的线程池  固定线程数的线程池
    private ExecutorService executorService = Executors.newFixedThreadPool(5);

    //JDK提供的线程池  定时线程池
    private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);
    //spring的普通线程池
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    //spring提供的定时线程池
    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    DiscussPostMapper discussPostMapper;


    //休眠
    private void sleep(long time) {
        try {
            Thread.sleep(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //1.jdk提供的固定数量的线程池
    @Test
    public void text01() {

//        ThreadPoolExecutor executor = new ThreadPoolExecutor();

        Runnable task = new Runnable() {
            @Override
            public void run() {
                log.info("线程的名称:" + Thread.currentThread().getName());
            }
        };

        executorService.submit(task);

        sleep(10000);
        executorService.submit(task);

        sleep(10000);

        executorService.submit(task);

        sleep(10000);
        executorService.submit(task);

        sleep(100000);
        executorService.submit(task);
    }

    //2.jdk提供的定时线程池
    @Test
    public void text02() throws InterruptedException {

        Runnable task = new Runnable() {
            @Override
            public void run() {
                System.out.println("当前线程池的名字:" + Thread.currentThread().getName());
            }
        };

        scheduledExecutorService.scheduleAtFixedRate(task, 1000, 10000, TimeUnit.MILLISECONDS);
    }


    //3.spring的普通线程池
    @Test
    public void text03() {

        Runnable task = new Runnable() {

            @Override
            public void run() {
                log.info("线程的名字"+Thread.currentThread().getName());
            }
        };

        threadPoolTaskExecutor.submit(task);
        threadPoolTaskExecutor.submit(task);
        threadPoolTaskExecutor.submit(task);
        threadPoolTaskExecutor.submit(task);

    }

    //4.spring的定时线程池
    @Test
    public void text04() {
        threadPoolTaskScheduler.execute(()->{
            System.out.println();
        });
    }



    //测试set容器
    @Test
    public void RedisSet() {

        redisTemplate.opsForSet().add("myset",1);
        redisTemplate.opsForSet().add("myset",2);
        redisTemplate.opsForSet().add("myset",3);
        BoundSetOperations myset = redisTemplate.boundSetOps("myset");

        System.out.println(myset.pop());

    }


    @Test
    public void textDate() throws ParseException {

        System.out.println(new SimpleDateFormat("yyyy-MM-dd").parse("2012-12-12"));

        System.out.println("当前时间秒数:"+new Date().getTime());

        System.out.println("当前是时间天数"+(new Date().getTime())/(1000*60*60*24));

    }

    @Test
    public void textUpdateDiscussPostScore() {

        discussPostMapper.updateDiscussPostScore(109,1000.00);

    }



}
